<script>
  const data = {
    appleForm: {
      id: 'value_id',
      name: 'value_name',
      ext: {
        extId: 'value_extId',
        extName: 'value_extName'
      },
      children: [
        {
          childId: 'value_childId',
          childName: 'value_childName',
          results: [
            { resultChildId: 'value_resultChildId' },
            { resultChildId: 'value_resultChildId2' }
          ]
        },
        { childId: 'value_childId2' }
      ]
    }
  }
  const toFormValues = {
    parseField(orgField) {
      const list = []
      const parse = (orgField) => {
        const o = { orgField, rule: null, index: null };
        [/\[\.\]/g, /\./g, /\[\d\]/g].forEach(reg => {
          if (!reg.test(orgField)) return
          if (!o.index || reg.lastIndex < o.index) {
            o.rule = reg
            o.index = reg.lastIndex
          }
        })
        const cur = orgField.substr(0, o.index)
        o.field = cur.replace(o.rule, '')
        o.nextField = orgField.substr(o.index, orgField.length)
        if (o.rule) {
          o.reference = String(cur.match(o.rule)[0]).replace(/\[|\]/g, '')
        }
        list.push(o)
        if (o.rule && o.nextField) {
          parse(o.nextField)
        }
      }
      parse(orgField)
      return list
    },
    getFormValue(fieldPath, source) {
      if (!fieldPath || !source) return
      const fieldList = toFormValues.parseField(fieldPath)
      if (!fieldList) return
      let org = source
      fieldList.forEach(({ field, orgField, reference }) => {
        org = org[field || orgField]
        if (!isNaN(reference) && org) {
          org = org[reference]
        }
      })
      return org
    },
    setFormValue({ fieldPath, value, source }) {
      if (!fieldPath || value === undefined || value === null) return
      source = source || {}
      const fieldList = toFormValues.parseField(fieldPath)
      if (!fieldList) return
      let org = source
      const list = fieldList.map(o => {
        const { field, orgField, reference } = o
        if (!field) {
          org[orgField] = value
          return org
        }
        org = org[field]
        if (!isNaN(reference)) {
          if (!org) {
            org = []
          }
          if (!org[reference]) {
            org[reference] = {}
          }
          org = org[reference]
        }
        return org
      })
      console.log(JSON.stringify(list[0], null, '\t'))
      return list[0]
    }
  }
    ;[
      // 'appleForm',
      // 'appleForm.id',
      // 'appleForm.ext.extName',
      // 'appleForm.children[0]childId',
      // 'appleForm.children[.]childId',
      'appleForm.children[0]results[1]resultChildId',
      // 'appleForm.children[0]results[.]resultChildId',
    ].forEach((k) => {
      // console.log(toFormValues.parseField(k))
      // console.log(
      //   toFormValues.parseField(k).map(({ field, orgField }) => field || orgField)
      // )
    })

    ;[
      // 'appleForm',
      // 'appleForm.id',
      // 'appleForm.ext.extName',
      'appleForm.children[0]childId',
      // 'appleForm.children[.]childId',
      // 'appleForm.children[0]results[1]resultChildId',
      // 'appleForm.children[0]results[.]resultChildId',
    ].forEach((k) => {
      // const list = toFormValues.parseField(k)
      // console.log(list, list.map(({ field, orgField }) => field || orgField))
      // console.log(
      //   toFormValues.getFormValue(
      //     k,
      //     data
      //   )
      // )
    })


    ;[
      // 'appleForm',
      // 'appleForm.id',
      // 'appleForm.ext.extName',
      // 'appleForm[.]childId',
      // 'appleForm.children[.]childId',
      'appleForm.children[1]childId',
      // 'appleForm.children[0]results[1]resultChildId',
      // 'appleForm.children[1]results[1]resultChildId',
      // 'appleForm.children[0]results[.]resultChildId',
    ].forEach(key => {
      const a = toFormValues.setFormValue({
        fieldPath: key,
        value: ['更新-值', '更新-值2'],
        source: JSON.parse(JSON.stringify(data))
      })
      // console.log('\n')
      // console.log(key)
      // console.log(JSON.stringify(a, null, '\t'))
    })

</script>